package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmAiTool;
import com.heima.wemedia.mapper.WmAiToolMapper;
import com.heima.wemedia.service.WmAiToolService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class WmAiToolServiceImpl extends ServiceImpl<WmAiToolMapper, WmAiTool> implements WmAiToolService {
    @Override
    public ResponseResult listByCategory(Integer categoryId) {
        List<WmAiTool> list=list(new LambdaQueryWrapper<WmAiTool>()
                .eq(WmAiTool::getCategoryId,categoryId)
                .eq(WmAiTool::getStatus,1)
                .orderByAsc(WmAiTool::getOrd)
        );
        return ResponseResult.okResult(list);
    }

    public ResponseResult listHotTools() {
        List<WmAiTool> list = list(new LambdaQueryWrapper<WmAiTool>()
                .eq(WmAiTool::getIsHot, true)
                .eq(WmAiTool::getStatus, 1)
                .orderByDesc(WmAiTool::getViewCount)
                .last("LIMIT 10"));
        return ResponseResult.okResult(list);
    }
    @Override
    @Transactional
    public ResponseResult scoreTool(Integer toolId, Integer score) {
        // 参数校验
        if (toolId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (score == null || score < 1 || score > 5) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评分值必须在1-5之间");
        }

        // 检查工具是否存在且状态正常
        WmAiTool tool = getById(toolId);
        if (tool == null || tool.getStatus() != 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "AI工具不存在或已下架");
        }

        // 更新评分统计信息
        // 增加评分人数
        int newScoreCount = tool.getScoreCount() + 1;
        // 增加总分
        int newScoreTotal = tool.getScoreTotal() + score;
        // 计算新的平均分
        BigDecimal newAvgScore = new BigDecimal(newScoreTotal)
                .divide(new BigDecimal(newScoreCount), 2, RoundingMode.HALF_UP);

        // 更新工具的评分统计数据
        WmAiTool updateTool = new WmAiTool();
        updateTool.setId(toolId);
        updateTool.setScoreCount(newScoreCount);
        updateTool.setScoreTotal(newScoreTotal);
        updateTool.setAvgScore(newAvgScore);

        updateById(updateTool);

        return ResponseResult.okResult(null);
    }
    @Override
    public ResponseResult getToolDetail(Integer id) {
        // 参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 查询工具详情
        WmAiTool tool = getById(id);
        if (tool == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "AI工具不存在");
        }

        // 只有上架状态的工具才能查看详情
        if (tool.getStatus() != 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "AI工具已下架");
        }

        // 增加浏览次数
        WmAiTool updateTool = new WmAiTool();
        updateTool.setId(id);
        updateTool.setViewCount(tool.getViewCount() + 1);
        updateById(updateTool);

        // 重新查询以获取更新后的浏览次数
        WmAiTool updatedTool = getById(id);

        return ResponseResult.okResult(updatedTool);
    }
}
